package com.wechat.server.mp.common.exception;

import com.wechat.server.mp.config.WechatConfigProperties;
import com.wechat.server.mp.util.ErrorEnum;
import com.wechat.server.mp.util.R;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @description: 统一异常拦截
 * @author: stone
 * @date: Created by 2021/11/15 14:42
 * @version: 1.0.0
 * @pakeage: com.wechat.server.mp.common.exception
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(value =NullPointerException.class)
    @ResponseBody
    public R exceptionHandler(NullPointerException e){
        log.error("发生空指针异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.ERROR.getCode(),ErrorEnum.ERROR.getDescription());
    }

    @ExceptionHandler(value =HasSignException.class)
    @ResponseBody
    public R exceptionHandler(HasSignException e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.HAS_SIGN.getCode(),ErrorEnum.HAS_SIGN.getDescription());
    }

    @ExceptionHandler(value =HasVoteException.class)
    @ResponseBody
    public R exceptionHandler(HasVoteException e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.ERROR.getCode(),ErrorEnum.ERROR.getDescription());
    }

    @ExceptionHandler(value =LotteryException.class)
    @ResponseBody
    public R exceptionHandler(LotteryException e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.ERROR_LOTTERY.getCode(), e.getMessage());
    }

    @ExceptionHandler(value =NotStartException.class)
    @ResponseBody
    public R exceptionHandler(WechatConfigProperties properties,NotStartException e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.NOT_START.getCode(), properties.getWeiyaTime());
    }

    @ExceptionHandler(value =ServerInternalException.class)
    @ResponseBody
    public R exceptionHandler(ServerInternalException e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.ERROR.getCode(),ErrorEnum.ERROR.getDescription());
    }

    @ExceptionHandler(value =VoteException.class)
    @ResponseBody
    public R exceptionHandler(VoteException e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.ERROR_VOTE.getCode(),ErrorEnum.ERROR_VOTE.getDescription());//R.error("ErrorEnum.ERROR_VOTE");
    }



    @ExceptionHandler(value = WxErrorException.class)
    @ResponseBody
    public R exceptionHandler(WxErrorException e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.ERROR_VOTE.getCode(),ErrorEnum.ERROR_VOTE.getDescription());//R.error("ErrorEnum.ERROR_VOTE");
    }


    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public R exceptionHandler(Exception e){
        log.error("发生异常！原因是: {}",e.getMessage());
        return R.error(ErrorEnum.ERROR_VOTE.getCode(),ErrorEnum.ERROR_VOTE.getDescription());//R.error("ErrorEnum.ERROR_VOTE");
    }


}
